﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace wlwthumb
{
    public partial class CropForm : Form
    {
        private Rectangle selectionRectangle;
        private int shiftW;

        public int ShiftW
        {
            get { return shiftW; }
            set { shiftW = value; }
        }
        private int shiftH;

        public int ShiftH
        {
            get { return shiftH; }
            set { shiftH = value; }
        }

        public Rectangle SelectionRectangle
        {
            get { return selectionRectangle; }
            set { 
                selectionRectangle = value;
                pictureBox1.Invalidate();
            }
        }
        public CropForm()
        {
            InitializeComponent();
        }

        internal void MakeSelection(int myWidth, int myHeight, Image img)
        {
            pictureBox1.Image = img;

            int w = pictureBox1.Image.Width;
            int h = pictureBox1.Image.Height;

            selectionRectangle = new Rectangle(0,0,w,h);
            if (w > h)
            {
                // landscape
                shiftW = w / 2 - h / 2;
                shiftH = 0;
                selectionRectangle = new Rectangle(shiftW,shiftH,h,h);
            }
            else
            {
                // portrait
                shiftW = 0;
                shiftH = h / 2 - w / 2;
                selectionRectangle = new Rectangle(shiftW, shiftH, w, w);
            }

            pictureBox1.Invalidate();

        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            using (Pen p = new Pen(Color.Red, 1))
            {
               e.Graphics.DrawRectangle(p,selectionRectangle);
            }
        }
    }
}
